import { EActionCode } from "framework/FSM/Action/ActionCode";
import { ActionBase, ActionBaseOfCreep } from "../ActionBase";
/**
 * 红绿一体机 攻击行为
 */
export class ActionIADAttackRed  extends ActionBaseOfCreep<IActionCMD<"attackRed">>
{
    protected updateArgs(action: IActionCMD<"attackRed">): EActionCode 
    {
        if(super.updateArgs(action) !== EActionCode.OK)
        {
            return EActionCode.FAILED;
        }
        action.arg.enemy = Game.getObjectById( action.arg.enemy.id)! as AttackObject
        if(!action.arg.enemy)
        {
            return EActionCode.FAILED;
        }
        return EActionCode.OK;
    }
    protected _execute(action: IActionCMD<"attackRed">): ActionCode 
    {
         
        const creep = action.arg.entity;
        if((action.arg.enemy as any)?.body)
        {
            creep.mymoveTo({pos:action.arg.enemy.pos,range:0},{});
        }
        else
        {
            creep.mymoveTo({pos:action.arg.enemy.pos,range:1},{});
        }
       
        if(creep.bodyCount(HEAL) && (creep.pos.distance(action.arg.enemy.pos)>1 ||  creep.getActiveBodyparts("attack")==0)   )
        {
            creep.heal(creep);
        }
        else
        {
            creep.attack(action.arg.enemy);
        }
        if(creep.bodyCount(RANGED_ATTACK))
        {
            if(creep.pos.distance(action.arg.enemy.pos)>1 && creep.pos.distance(action.arg.enemy.pos)<=3)
            {
                creep.rangedAttack(action.arg.enemy)
            }
            else
            {
                creep.rangedMassAttack()
            }
        }
        return  EActionCode.OK;
    }
}